package com.furnitur.modules.portal.service.impl;

import com.furnitur.common.exception.BusinessException;
import com.furnitur.modules.portal.mapper.PortalCategoryMapper;
import com.furnitur.modules.portal.service.PortalCategoryService;
import com.furnitur.modules.portal.vo.category.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PortalCategoryServiceImpl implements PortalCategoryService {

    @Autowired
    private PortalCategoryMapper categoryMapper;

    @Override
    @Cacheable(value = "category", key = "'all'")
    public List<CategoryVO> listAllCategories() {
        log.info("Loading all categories");
        List<CategoryVO> categories = categoryMapper.selectAllCategories();
        log.info("Loaded {} categories", categories.size());
        return categories;
    }

    @Override
    @Cacheable(value = "category", key = "#id")
    public CategoryVO getCategoryById(Long id) {
        log.info("Loading category by id: {}", id);
        CategoryVO category = categoryMapper.selectCategoryById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        return category;
    }
} 